# file writes out an index of installed gems rdocs so you can browse to them easily

require 'rubygems'
# ltodo: see how RI is able to pull in everything, see if I could do the same--where is it getting that info?

outfile = File.open 'all_gems_rdocs_index.html', 'wb'
outfile.write("<HTML><HEAD> <TITLE>All existing gem rdocs</TITLE> </HEAD>")

outfile.write "<BODY>"
outfile.write("All installed gems: ")
outfile.write("<a href=\"http://www.ruby-doc.org/core/\">(core)</a> <BR />")
outfile.write "<BR />"

Gem.path.each do |gem_path|
  all_gems_dirs = Dir.glob(gem_path  + '/gems/*') 
  all_gems_dirs.each do |gem_version_full_path|
    name_dash_version = gem_version_full_path.split("/")[-1]
    name = name_dash_version.split('-')[0..-2].join('-')
    version = name_dash_version.split('-')[-1]
    begin
      gem name, version
    rescue Gem::LoadError, Gem::Exception, ArgumentError # can't load things twice!
    end

    spec = Gem.loaded_specs[name]
    outfile.write " #{name_dash_version}: "  
    separator = "&nbsp;" 
    if spec && spec.homepage 
	outfile.write("<a href=\"#{spec.homepage}\">homepage</a> #{separator}")
    end

    if spec && spec.has_rdoc
        outfile.write(%! <a href="#{Dir.glob(gem_path + '/doc/' + name_dash_version + '/rdoc/index.html')[0]}">rdoc</a> #{separator}! )
    else
	outfile.write(" (no rdoc) ")
    end

    if File.exist? gem_version_full_path + '/README'
       outfile.write("<a href=\"#{gem_version_full_path + '/README'}\"> README</a>")
    end

    outfile.write "<HR />"

  end
end 


outfile.write("</BODY></HTML>")
outfile.close
print 'wrote to ', outfile.path, "\n"
